import API from "../../api/index";
import valid from '../../utils/validate'
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone: '',//手机号码
    hiddenmodalput: true,//绑定手机号模态框显示状态
    userInfo: ''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  //跳转成长记录
  skip(){
    wx.switchTab({
      url: "/pages/record/index",
    })
  },
  about(){
    wx.navigateTo({
      url: '/pages/about/index',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this._getUsers()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //获取用户信息
  _getUsers() {
    let that = this
    API.getUsers().then(res => {
      console.log('获取用户信息', res)
      let userInfo = res.data.user
      app.globalData.userInfo = userInfo
      that.setData({ userInfo })
    })
  },
  //更新用户信息
  handleGetUserInfo(e) {
    let that = this
    let tempData = e.detail.userInfo
    console.log('tempData', tempData)
    API.updateUsers({
      head: tempData.avatarUrl,
      nickname: tempData.nickName,
      phone: ''
    }).then(res => {
      that._getUsers()
    })
  },
  //绑定手机号码
  getPhoneNumber(e) {
    let that = this
    console.log(e.detail.encryptedData)
    if (e.detail.errMsg == "getPhoneNumber:ok") {
      API.binding({
        encryptedData: e.detail.encryptedData,
        iv: e.detail.iv
      }).then(res => {
        console.log(res.data.phoneNumber)
        let phoneNumber = res.data.phoneNumber
        let tempData = app.globalData.userInfo
        console.log('tempData', tempData)
        API.updateUsers({
          head: tempData?tempData.avatarUrl:'',
          nickname: tempData?tempData.nickName:'',
          phone: tempData?phoneNumber:''
        }).then(res => {
          that._getUsers()
          that.cancel()
        })
      })
    }
  },
  //绑定手机号的模态框显示状态
  // modalinput: function () {
  //   this.setData({
  //     hiddenmodalput: !this.data.hiddenmodalput
  //   })
  // },
  // //绑定手机号的模态框-取消
  cancel() {
    this.setData({
      hiddenmodalput: !this.data.hiddenmodalput
    })
  },
  // //绑定手机号的模态框-确定
  // confirm() {
  //   let that = this
  //   console.log(valid.isMobile(this.data.phone))
  //   if (this.data.phone && valid.isMobile(this.data.phone)) {
  //     let tempData = app.globalData.userInfo
  //     console.log('tempData', tempData)
  //     API.updateUsers({
  //       head: tempData.avatarUrl,
  //       nickname: tempData.nickName,
  //       phone: this.data.phone
  //     }).then(res => {
  //       that._getUsers()
  //       that.cancel()
  //     })
  //   } else {
  //     wx.showToast({
  //       icon: 'none',
  //       title: '请完善信息',
  //     })
  //   }
  // },
  //获取input手机号码
  phoneInput: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },
})